//
//  TransactionQueryViewController.m
//  JimiPay
//
//  Created by yull on 16/1/7.
//  Copyright (c) 2016年 com.shengyun. All rights reserved.
//

#import "TransactionQueryViewController.h"
#import "TDTransaction.h"
#import "SVPullToRefresh.h"
#import "TDTransactionDetailViewController.h"

@interface TransactionQueryViewController ()

@property (nonatomic, strong) NSMutableArray *records;
@property (nonatomic, assign) NSUInteger start;

@end

@implementation TransactionQueryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.title = @"交易查询";
    [self setupCustomBackItem];
    
    self.records = [NSMutableArray array];
    self.tableView.tableFooterView = [[UIView alloc] init];
    __weak typeof(self) wself = self;
    
    [self.tableView addPullToRefreshWithActionHandler:^{
        wself.start = 0;
        [wself loadData];
    }];
    
    [self.tableView addInfiniteScrollingWithActionHandler:^{
        [wself loadData];
    }];
    
    [self loadData];
}

- (void)loadData
{
    [TDHttpEngine requestFor_GETTRANSERIALLIST_WithCustId:[[NSUserDefaults standardUserDefaults] objectForKey:CUST_ID] custMobile:[[NSUserDefaults standardUserDefaults] objectForKey:CUST_MOBILE] start:self.start pageSize:10 complete:^(BOOL succeed, NSString *msg, id reqBody) {

        [self.tableView.pullToRefreshView stopAnimating];
        [self.tableView.infiniteScrollingView stopAnimating];
        
        if (succeed) {
            NSArray *tranList = [reqBody objectForKey:@"tranList"];
            
            if (self.start == 0) {
                [self.records removeAllObjects];
            }
            
            [tranList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                TDTransaction *transaction = [TDTransaction transactionWithDictionary:obj];
                [self.records addObject:transaction];
            }];
            
            [self.tableView reloadData];
            self.start += tranList.count;
            
            if (tranList.count < 10) {
                self.tableView.showsInfiniteScrolling = NO;
                if (self.records.count == 0) {
                    self.blankView.hidden = NO;
                }
            }
        } else {
            [self.view makeToast:msg duration:2.0f position:@"center"];
        }
    }];
}

#pragma mark - UITableViewDataSource, UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.records.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"TransactionQueryIdentifier";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        
        UILabel *recordTypeL = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 150, 20)];
        recordTypeL.font = [UIFont systemFontOfSize:17];
        recordTypeL.tag = 1001;
        [cell.contentView addSubview:recordTypeL];
        
        UILabel *recordAmtL = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(recordTypeL.frame), 10, self.view.frame.size.width - 10 - CGRectGetMaxX(recordTypeL.frame), 20)];
        recordAmtL.font = [UIFont systemFontOfSize:17];
        recordAmtL.textAlignment = NSTextAlignmentRight;
        recordAmtL.tag = 1002;
        [cell.contentView addSubview:recordAmtL];
        
        UILabel *recordNoL = [[UILabel alloc] initWithFrame:CGRectMake(10, 40, 200, 20)];
        recordNoL.font = [UIFont systemFontOfSize:14];
        recordNoL.textColor = [UIColor lightGrayColor];
        recordNoL.tag = 1003;
        [cell.contentView addSubview:recordNoL];
        
        UILabel *recordTimeL = [[UILabel alloc] initWithFrame:CGRectMake(10, 70, 200, 20)];
        recordTimeL.font = [UIFont systemFontOfSize:14];
        recordTimeL.textColor = [UIColor lightGrayColor];
        recordTimeL.tag = 1004;
        [cell.contentView addSubview:recordTimeL];
        
        UILabel *recordStatusL = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(recordTimeL.frame), 70, self.view.frame.size.width - 10 - CGRectGetMaxX(recordTimeL.frame), 20)];
        recordStatusL.textAlignment = NSTextAlignmentRight;
        recordStatusL.font = [UIFont systemFontOfSize:14];
        recordStatusL.textColor = [UIColor lightGrayColor];
        recordStatusL.tag = 1005;
        [cell.contentView addSubview:recordStatusL];
    }
    
    TDTransaction *transaction = self.records[indexPath.row];
    
    UILabel *recordTypeL = (UILabel *)[cell.contentView viewWithTag:1001];
    recordTypeL.text = [transaction get_prdordtype_string];
    UILabel *recordAmtL = (UILabel *)[cell.contentView viewWithTag:1002];
    recordAmtL.text = transaction.ordamt;
    UILabel *recordNoL = (UILabel *)[cell.contentView viewWithTag:1003];
    recordNoL.text = transaction.prdordno;
    UILabel *recordTimeL = (UILabel *)[cell.contentView viewWithTag:1004];
    recordTimeL.text = [transaction get_ordtime_format];
    UILabel *recordStatusL = (UILabel *)[cell.contentView viewWithTag:1005];
    recordStatusL.text = [transaction get_ordstatus_string];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    TDTransaction *transaction = self.records[indexPath.row];
    
    TDTransactionDetailViewController *controller = [[TDTransactionDetailViewController alloc]init];
    controller.payordno = transaction.payordno;
    controller.orderTime = transaction.ordtime;
    controller.paystatus = [transaction get_ordstatus_string];
    controller.category = [transaction get_prdordtype_string];
    [self.navigationController pushViewController:controller animated:YES];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [cell setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [cell setLayoutMargins:UIEdgeInsetsZero];
        
    }
}

@end
